package cn.wolfcode.web.controller;


import cn.wolfcode.domain.ClassInfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.ClassInfoQueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.service.IClassInfoService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassInfoController {

    @Autowired
    private IClassInfoService classInfoService;

    @Autowired
    private IEmployeeService employeeService;


    @RequiresPermissions(value = {"classInfo:list", "班级列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ClassInfoQueryObject qo) {
        PageInfo<ClassInfo> pageInfo = classInfoService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        // 查询所有部门数据
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees", employees);

        return "classinfo/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"classInfo:delete", "班级删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            classInfoService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"classInfo:saveOrUpdate", "班级新增/修改"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(ClassInfo classInfo) {
        if (classInfo.getId() != null) {
            classInfoService.update(classInfo);
        } else {
            classInfoService.save(classInfo);
        }
        return new JSONResult();
    }

}
